clear all
close all
addpath img2raw
addpath raw2fmt
addpath fmt2rlt
addpath rlt2img

% get all origin images name
inputFolder = fullfile('..\\images\\origin');
outputFolder = fullfile('..\\images\\result');
dirJpgInfo = dir(fullfile(inputFolder,'*.jpg'));
dirPngInfo = dir(fullfile(inputFolder,'*.png'));
dirImgInfo = [dirJpgInfo;dirPngInfo];

success = [];
fail = [];


siz = size(dirImgInfo);
for i = 1:siz(1)
    tic

    t = timer;
    if i == 1
        t.StartDelay = 15;    % 第一次 15s 后超时，为初始化留余地
    else
        t.StartDelay = 5;     % 5s 后超时
    end
    t.TimerFcn = @(~,~) throw(MExpextion("time out"));
    start(t);
    imgName = dirImgInfo(i).name;

    try
        % disp(dirJpgInfo(i).name);
        inputPath = [pwd, '\', inputFolder, '\', imgName];
        % tic
        fprintf("checking img [%s]\n", inputPath);
        raw = pictureSplit(inputPath);
        % toc

        % tic
        % disp("foamatting")
        fmt = formatGraph(raw);
        % toc

        % tic
        % disp("solving")
        rlt = solveSudoku(fmt);
        % toc

        % tic
        outputPath = [pwd, '\', outputFolder, '\', imgName];
        fprintf("saving result to [%s]\n", outputPath);
        printGraph(rlt, outputPath);
        % toc
        
        success = [success;imgName];
        disp("success!")
    catch ME
        fail = [fail;imgName];
        disp("EXCEPTION!!");
        disp(ME.message);
        for i = 1:numel(ME.stack)
            st = ME.stack(i);
            fprintf("in file %s \n\t func %s \n\t line %d \n", st.file, st.name, st.line);
        end
    end
    stop(t)
    delete(t);
    toc
end

success
fail